# No SUBDIR variable here...
# The 'test' subdir is added via src/Makefile to handle
# it's dependency on src/engine

lib_LTLIBRARIES = libgnc-core-utils.la

libgnc_core_utils_la_SOURCES = \
  binreloc.c \
  gnc-prefs.c \
  gnc-environment.c \
  gnc-filepath-utils.cpp \
  gnc-gkeyfile-utils.c \
  gnc-glib-utils.c \
  gnc-guile-utils.c \
  gnc-jalali.c \
  gnc-locale-utils.c \
  gnc-path.c \
  swig-core-utils-guile.c

libgnc_core_utils_la_LIBADD = \
  ${GUILE_LIBS} \
  ${GLIB_LIBS} \
  ${BINRELOC_LIBS} \
  ${GTK_MAC_LIBS} \
  ${BOOST_LDFLAGS} -lboost_filesystem


noinst_HEADERS = \
  binreloc.h \
  gnc-prefs.h \
  gnc-prefs-p.h \
  gnc-environment.h \
  gnc-filepath-utils.h \
  gnc-gkeyfile-utils.h \
  gnc-glib-utils.h \
  gnc-guile-utils.h \
  gnc-jalali.h \
  gnc-locale-utils.h \
  gnc-path.h

if BUILDING_FROM_VCS
swig-core-utils-guile.c: core-utils.i ${top_srcdir}/common/base-typemaps.i
	$(SWIG) -guile -Linkage module \
	-I${top_srcdir}/common -o $@ $<
if ! OS_WIN32
if ! SWIG_DIST_FAIL
	if ! `grep "define scm_from_utf8_string" $@ > /dev/null 2>&1`; then \
	  patch $@ $(top_srcdir)/common/swig-utf8.patch; \
	fi
endif
endif
swig-core-utils-python.c: core-utils.i ${top_srcdir}/common/base-typemaps.i
	$(SWIG) -python -Wall -Werror \
	-I${GLIB_CFLAGS} -I${top_srcdir}/common -I${srcdir} -o $@ $<
endif

AM_CPPFLAGS = \
  ${GUILE_CFLAGS} \
  ${GLIB_CFLAGS} \
  -I${top_builddir}/common \
  -I${top_srcdir}/common \
  $(BOOST_CPPFLAGS)

AM_CFLAGS = ${GTK_MAC_CFLAGS}
AM_CXXFLAGS = ${GTK_MAC_CXXFLAGS}

gncscmmoddir = ${GNC_SCM_INSTALL_DIR}/gnucash
gncscmmod_DATA = core-utils.scm

if GNUCASH_SEPARATE_BUILDDIR
#Only needed when srcdir and builddir are different
#for running
SCM_FILE_LINKS = ${gncscmmod_DATA}
endif

if WITH_PYTHON

lib_LTLIBRARIES += libgnc-core-utils-python.la

libgnc_core_utils_python_la_SOURCES = \
	swig-core-utils-python.c

libgnc_core_utils_python_la_LIBADD = \
	${PYTHON_LIBS} \
	libgnc-core-utils.la

libgnc_core_utils_python_la_CPPFLAGS = \
  ${GLIB_CFLAGS} \
  ${GTK_MAC_CFLAGS} \
  ${PYTHON_CPPFLAGS} \
  -I${top_builddir}/common \
  -I${top_srcdir}/common

endif

.scm-links: 
	$(RM) -rf gnucash
	mkdir -p  gnucash
if GNUCASH_SEPARATE_BUILDDIR
	for X in ${SCM_FILE_LINKS} ; do \
	  $(LN_S) -f ${srcdir}/$$X . ; \
	done
endif
	( cd gnucash; for A in $(gncscmmod_DATA) ; do $(LN_S) -f ../$$A . ; done )
if ! OS_WIN32
# Windows knows no "ln -s" but uses "cp": must copy every time (see bug #566567).
	touch .scm-links
endif

if GNC_HAVE_GUILE_2
GUILE_COMPILE_ENV = \
  --library-dir    ${top_builddir}/libgnucash/core-utils

%.go : %.scm .scm-links $(lib_LTLIBRARIES)
	GNC_UNINSTALLED=yes \
	GNC_BUILDDIR=${top_builddir} \
	$(shell ${abs_top_srcdir}/common/gnc-test-env.pl --noexports ${GUILE_COMPILE_ENV})  \
	$(GUILD) compile -o $@ $<

gncscmmodcachedir = ${pkglibdir}/scm/ccache/@GUILE_EFFECTIVE_VERSION@/gnucash
gncscmmodcache_DATA = $(gncscmmod_DATA:.scm=.go)
endif

noinst_DATA = .scm-links
BUILT_SOURCES = gncla-dir.h gnc-version.h
EXTRA_DIST = \
  $(gncscmmod_DATA) \
  core-utils.i \
  gncla-dir.h.in \
  gnc-vcs-info.h \
  CMakeLists.txt

clean-local:
	rm -rf gnucash

CLEANFILES = $(BUILT_SOURCES) .scm-links ${gncscmmodcache_DATA}

MAINTAINERCLEANFILES = swig-core-utils-guile.c swig-core-utils-python.c

AM_CPPFLAGS += -DG_LOG_DOMAIN=\"gnc.core-utils\" -DGNC_SCM_INSTALL_DIR="\"${GNC_SCM_INSTALL_DIR}\""

gncla-dir.h: gncla-dir.h.in ${top_builddir}/config.status Makefile
	rm -f $@.tmp
	sed < $< > $@.tmp \
		-e 's#@-DATADIRNAME-@#${DATADIRNAME}#g' \
		-e 's#@-libdir-@#${libdir}#g' \
		-e 's#@-bindir-@#${bindir}#g' \
		-e 's#@-sysconfdir-@#${sysconfdir}#g' \
		-e 's#@-datadir-@#${datadir}#g' \
		-e 's#@-prefix-@#${prefix}#g'
	mv $@.tmp $@

## We borrow guile's convention and use @-...-@ as the substitution
## brackets here, instead of the usual @...@.  This prevents autoconf
## from substituting the values directly into the left-hand sides of
## the sed substitutions.
gnc-version.h: _gnc-version.h
	-if [ ! -f gnc-version.h ]; then cp _gnc-version.h gnc-version.h; fi
	-cmp -s _gnc-version.h gnc-version.h || cp _gnc-version.h gnc-version.h
	-rm -f _gnc-version.h

_gnc-version.h: gnc-vcs-info.h Makefile
	@echo "/* Autogenerated. Do not change. */"   > _gnc-version.h
	@echo "#ifndef GNC_VERSION_H"                >> _gnc-version.h
	@echo "#define GNC_VERSION_H"                >> _gnc-version.h
	@echo ""                                     >> _gnc-version.h
	@scm_info=`${top_srcdir}/util/gnc-vcs-info -t ${top_srcdir}` ; \
	if [ $$? = 0 ] ; then \
		echo "#define GNC_VCS \"$$scm_info\"" >> _gnc-version.h ; \
	fi
	@echo "#include \"gnc-vcs-info.h\""          >> _gnc-version.h
	@echo "#endif"                               >> _gnc-version.h

if BUILDING_FROM_VCS
gnc-vcs-info.h: _gnc-vcs-info.h
	-if [ ! -f gnc-vcs-info.h ]; then cp _gnc-vcs-info.h gnc-vcs-info.h; fi
	-cmp -s _gnc-vcs-info.h gnc-vcs-info.h || cp _gnc-vcs-info.h gnc-vcs-info.h
	-rm -f _gnc-vcs-info.h

_gnc-vcs-info.h: Makefile
	@scm_info=`${top_srcdir}/util/gnc-vcs-info -r ${top_srcdir}` ; \
	info_result=$$? ; \
	scm_date=`${top_srcdir}/util/gnc-vcs-info -d ${top_srcdir}` ; \
	date_result=$$? ; \
	if [ $$info_result != 0 ] || [ $$date_result != 0 ] ; then \
		echo "You're building from svn/svk/git/bzr...  But your build system is broken" ; \
		echo "Don't do that.   Complain to your build-system creator." ; \
		exit 1 ; \
	else \
		if [ -z "$$scm_info" ] || [ -z "$$scm_date" ] ; then \
			echo "gnc-vcs-info failed.  figure out why." ; \
			echo "can't determine svn/svk/git/bzr revision from ${top_srcdir}." ; \
			exit 1 ; \
		fi ; \
		echo "/* Autogenerated. Do not change. */"    > _gnc-vcs-info.h ; \
		echo "#define GNC_VCS_REV \"$$scm_info\"" >> _gnc-vcs-info.h ; \
		echo "#define GNC_VCS_REV_DATE \"$$scm_date\"" >> _gnc-vcs-info.h ; \
		echo "#define GNC_VCS_REV_YEAR \"$${scm_date%%-*}\"" >> _gnc-vcs-info.h ; \
		echo "#define GNC_VCS_REV_Y_M \"$${scm_date%-*}\"" >> _gnc-vcs-info.h ; \
	fi
endif
